//
//  YAxisRenderer.swift
//  Charts
//
//  Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
//  A port of MPAndroidChart for iOS
//  Licensed under Apache License 2.0
//
//  https://github.com/danielgindi/Charts
//

import Foundation
import CoreGraphics

///自定义y轴分区背景颜色
//@objc(ChartYAxisCustomRenderer)
@objc open class YAxisCustomRenderer:YAxisRenderer
{
//    public var minF: Double = 3.9
//    public var maxF: Double = 7.8
    
    @objc public var minF: Double = 0.0
    @objc public var maxF: Double = 0.0
    
    open override func  renderGridLines(context: CGContext)
    {
        
        guard axis.isEnabled else { return }

        if axis.drawGridLinesEnabled
        {
            let positions = transformedPositions()
            
            context.saveGState()
            defer { context.restoreGState() }
            context.clip(to: self.gridClippingRect)
            
            context.setShouldAntialias(axis.gridAntialiasEnabled)
            context.setStrokeColor(axis.gridColor.cgColor)
            context.setLineWidth(axis.gridLineWidth)
            context.setLineCap(axis.gridLineCap)
            
            if axis.gridLineDashLengths != nil
            {
                context.setLineDash(phase: axis.gridLineDashPhase, lengths: axis.gridLineDashLengths)
            }
            else
            {
                context.setLineDash(phase: 0.0, lengths: [])
            }
            
            // draw the grid
            positions.forEach { drawGridLine(context: context, position: $0) }
            
//            //根据y轴的range去绘制背景色
            drawForRange(context: context, positions: positions)
        }

        if axis.drawZeroLineEnabled
        {
            // draw zero line
            drawZeroLine(context: context)
        }
    }

    // 根据y轴的range去绘制背景色
    func drawForRange(context: CGContext, positions: [CGPoint]) {
        
        let ranges: [[String: Any]] = [
                                       ["start": getYAxisStandardPosition(from: minF),
                                        "end": getYAxisStandardPosition(from: maxF),
                                        "color": UIColor(red:0.0/255.0, green:195.0/255.0, blue:194.0/255.0, alpha:0.05)]]
        
        // 开始填充
        for r in ranges {
            
            let start = (r["start"] as? CGPoint)?.y ?? 0.0
            let end = (r["end"] as? CGPoint)?.y ?? 0.0
            // 填充范围
            let rect :CGRect = CGRect(x: viewPortHandler.contentLeft,
                                      y: start,
                                      width: viewPortHandler.contentRight-viewPortHandler.contentLeft,
                                      height: end - start)
            
            
            let c: UIColor = r["color"] as! UIColor
            context.setFillColor(c.cgColor)
            context.fill(rect)
        }
    }
    
    // 取得Y轴数值的位置
    private func getYAxisStandardPosition(from value: Double) -> CGPoint {
        return transformer?.pixelForValues(x: 0, y: value) ?? CGPoint.zero
    }
}
